package com.hula.onion.book.server.spider.platforms.zhongrongbao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by work on 2018/1/23.
 */
@Component
public class ZhongRongBaoSpiderHelper {
    public Map<String, Object> installRequestParams(LoginForm form) {
        //登陆json
        JSONObject loginInfoJson = new JSONObject();
        loginInfoJson.put("loginId", form.getUserName());
        loginInfoJson.put("pwd", form.getPassword());
        loginInfoJson.put("isRemember", "false");
        //body json
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("loginInfo", loginInfoJson);
        //param json
        JSONObject paramJson = new JSONObject();
        paramJson.put("Body", bodyJson);
        //head json
        JSONObject headJson = new JSONObject();
        headJson.put("sid", "");
        headJson.put("channelType", "05");
        headJson.put("interfaceVersion", "1.0");
        headJson.put("funCode", "userLogin");
        paramJson.put("Head", headJson);
        Map<String, Object> result = new HashMap<>();
        result.put("dataMsg", paramJson);
        return result;
    }

    public Map<String, Object> installAccountParams(String sid) {
        //page json
        JSONObject pageInfoJson = new JSONObject();
        pageInfoJson.put("pageNum", "1");
        //param json
        JSONObject paramJson = new JSONObject();
        paramJson.put("queryId", "allinpayAccountNew");
        //body json
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("pageInfo", pageInfoJson);
        bodyJson.put("param", paramJson);
        //param json
        JSONObject queryJson = new JSONObject();
        queryJson.put("Body", bodyJson);
        //head json
        JSONObject headJson = new JSONObject();
        headJson.put("sid", sid);
        headJson.put("channelType", "01");
        headJson.put("interfaceVersion", "1.0");
        headJson.put("funCode", "dataQuery");
        queryJson.put("Head", headJson);
        Map<String, Object> result = new HashMap<>();
        result.put("dataMsg", queryJson);
        return result;
    }

    /**
     * 解析账户余额
     *
     * @param jsonStr
     * @return
     */
    public CommonResponse parseAccountBalance(String jsonStr) {
        if (StringUtils.isEmpty(jsonStr)) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        System.out.println(jsonObject);
        if ("SUCC".equals(jsonObject.getJSONObject("head").getString("state"))) {
            JSONArray jsonArray = jsonObject.getJSONObject("body").
                    getJSONArray("data");
            String balance = jsonArray.getJSONObject(0).
                    getString("totalAmount");
            CommonResponse commonResponse = new
                    CommonResponse(ResponseStatusEnum.SUCCESS);

            AccountInfo accountInfo = new AccountInfo();

            accountInfo.setAmount(new BigDecimal(balance));
            commonResponse.setData(accountInfo);
            return commonResponse;
        }
        return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
    }

}
